﻿Imports Control_Vuelos_BL
Imports DevExpress.XtraEditors.Controls

Public Class frmAdm_Asiento
    Inherits frmBaseMantenimiento

    ReadOnly vConciliaBL As New ConsolidadoBL()
    ReadOnly vMaestroBL As New MaestrosBL()
    Dim tbPeriodo As New DataTable

    Private Sub frmAdm_Asiento_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        AddHandler BarBtnGrabar.ItemClick, AddressOf prl_RegistroGrabar
        AddHandler UcAsiento1.btnBuscar.Click, AddressOf prl_btnClickBuscarTicket
        AddHandler UcAsiento1.cboPeriodo.GotFocus, AddressOf prl_Cbo_GotFocus
        BarBtnAgregar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnModificar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnEliminar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnRefresh.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnImprimir.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnCancelar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarBtnGrabar.LargeGlyph = Control_Vuelos_GUI.My.Resources.Resources.Process
        BarBtnGrabar.Caption = "Generar"
        prl_CargarCombos()
        UcAsiento1.cboPeriodo.EditValue = "201212"
    End Sub

#Region "Actualizacion de datos"

    Private Sub prl_RegistroGrabar()
        Dim row As DataRow = TryCast(UcAsiento1.GridView1.GetDataRow(UcAsiento1.GridView1.GetSelectedRows()(0)), DataRow)
        If Not row Is Nothing Then
            Dim snroticket As String = row("nroTicket")
            Dim snroPeriodo As String = row("nroPeriodo")
            Dim sCodAsiento As String = row("codAsiento")
            Dim sNroAsiento As String = row("nroAsiento")
            If vConciliaBL.pr_VerificarCierreContable(snroPeriodo) Then
                MsgBox(String.Format("El periodo {0} está cerrado en la contabilidad...", snroPeriodo), MsgBoxStyle.Information, Text)
            Else
                ' Genero el asiento
                Dim sAsiento As String = vConciliaBL.pr_GenerarAsiento(snroticket, snroPeriodo, sCodAsiento.Trim, sNroAsiento.Trim, p_coduser)
                ' cargo el numero de asiento en la grilla
                row("Asiento") = sAsiento
                row("nroAsiento") = Strings.Right(sAsiento, 4)
                MsgBox("El asiento se generó con éxito...", MsgBoxStyle.Information, Text)
            End If
        End If
    End Sub

#End Region

#Region "Miscelaneos"

    Private Sub prl_CargarCombos()
        tbPeriodo = vMaestroBL.pr_GetPeriodo()
        UcAsiento1.cboPeriodo.Properties.DataSource = tbPeriodo
        UcAsiento1.cboPeriodo.Properties.DisplayMember = "desmes"
        UcAsiento1.cboPeriodo.Properties.ValueMember = "anomes"
        UcAsiento1.cboPeriodo.Properties.Columns.Add(New DevExpress.XtraEditors.Controls.LookUpColumnInfo("anomes", 6, "Periodo"))
        UcAsiento1.cboPeriodo.Properties.Columns.Add(New DevExpress.XtraEditors.Controls.LookUpColumnInfo("desmes", 20, "Descripción"))
        UcAsiento1.cboPeriodo.Properties.BestFit()
        UcAsiento1.cboPeriodo.Properties.PopupWidth = 120
        UcAsiento1.cboPeriodo.Properties.SearchMode = SearchMode.AutoComplete
        UcAsiento1.cboPeriodo.Properties.AutoSearchColumnIndex = 1
    End Sub

    Private Sub prl_Cbo_GotFocus()
        Dim dt As New DataTable
        dt.Reset()
        UcAsiento1.GridControl1.DataSource = dt
        BarBtnGrabar.Enabled = False
    End Sub

    Private Sub prl_btnClickBuscarTicket()
        Dim sFiltro As String = String.Format("NroPeriodo = '{0}'", UcAsiento1.cboPeriodo.EditValue)
        Dim dtConsulta As DataTable = vConciliaBL.pr_ListarTickets(sFiltro)
        If dtConsulta.Rows.Count > 0 Then
            BarBtnGrabar.Enabled = True
            UcAsiento1.GridControl1.DataSource = dtConsulta
            UcAsiento1.GridControl1.Focus()
        Else
            MsgBox("No hay provisiones registradas en el mes requerido...", MsgBoxStyle.Exclamation, Text)
        End If
    End Sub

#End Region

End Class